svg形式のテキストを画像にするserverless function
repo
GitHub - takker99/svg-hosting: image/svg+xmlでないsvgテキストをimage/svg+xmlにして返すやつ
svg-hosting
APIの書式
https://svg-hosting.vercel.app/api/svg?url=$url
scrapboxに貼り付けたコードブロック記法の場合は以下も使える
https://svg-hosting.vercel.app/scrapbox.io/$project/$page/$filename
用途
scrapboxのコードブロック記法に書いたsvgコードを画像にしてscrapboxなどのサイトで使いたい
実装
Vercelでserverless functionを作る
https://github.com/takker99/svg-hosting
2020-12-03 03:06:18 うまく動いていない
vercel-denoで作り直そう
03:15:20 serverless functionの作動テスト終了
03:47:09 code書いた
簡単に書けた
03:55:34 deploy終了
https://svg-hosting.vercel.app/
error処理とかは全然してない
とりあえず動く
<script>が混じっていると危険かも
04:08:57 scrapbox向けに使えるoptionを用意する?
04:25:29 用意した
https://github.com/daiiz/svg-wh を参考にする
/daiiz/SVGのviewBoxをみてwidthとheightをつけるやつ
バグ
/villagepump/svg-hostingが一定確率で500 Internal Server Errorを吐く
2022-01-13
12:12:43 fix svg形式のテキストを画像にするserverless function#600cd7121280f00000be6ee8 https://github.com/takker99/svg-hosting/pull/1
known issue
/icons/done.icon更新が遅いみたい
from /villagepump/コードブロック記法に書いたsvgを表示する#5fd759795b98c20000c5332e
Cache-Controlの設定をいじる必要がある
/ci7lus/ツイートを画像にするやつを作ったが参考になりそう
ページ名が変わるとリンク切れになる
followRenameでなんとか行けないかな?
private projectに書いたsvgは無理
認証が足りない
テスト
Animationも出せる!!
https://svg-hosting.vercel.app/api/svg?url=https://scrapbox.io/api/code/takker/svg形式のテキストを画像にするserverless_function/test2.svg
元がimage/svg+xmlなら当然問題なし
https://svg-hosting.vercel.app/api/svg?url=https://twemoji.maxcdn.com/2/svg/1f980.svg
テキストだとうまく行かない?
preview deployでは画像がちゃんと返却されたんだけどな?
渡すurlを間違えているだけだった
https://svg-hosting.vercel.app/api/svg?url=https://scrapbox.io/api/code/takker/svg形式のテキストを画像にするserverless_function/test1.svg
ForeignObjectが実行されるかどうかのテスト
https://svg-hosting.vercel.app/scrapbox.io/takker/svg形式のテキストを画像にするserverless_function/dangerousSvg.svg
新しいタブで画像を開くと実行されるみたい
Scrapboxだけで見る分には問題なさそう
code:dangerousSvg.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="50" fill="red"/>
<foreignObject>
<script>alert("hello!");</script>
</foreignObject>
</svg>
from /svg-wiki/svgに埋め込まれたscriptを実行させない
code:test1.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<!--
パスコマンドや座標の区切りが判る範囲でスペースを省略,追加することが出来る.
ここではコマンド毎に改行を入れてみた.
-->
<path stroke="black" stroke-width="1" fill="none" d="
M 0 200
L 25 50
l 50 -25
V 175
H 100
v -50
h 25
m 25 0
l 50 0
L 175 175
z
"/>
</svg>
code:test2.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<path d="M 25 175 L 25 25" stroke="black" stroke-dasharray="2"/>
<path d="M 175 25 L 175 175" stroke="black" stroke-dasharray="2"/>
<path d="M 25 25 L 175 25" stroke="black" stroke-dasharray="2"/>
<path d="M 25 175 Q 25 25 175 25" stroke="black" stroke-dasharray="2" fill="none"/>
<path d="M 25 25 Q 175 25 175 175" stroke="black" stroke-dasharray="2" fill="none"/>
<path d="M 25 175 C 25 25 175 25 175 175" stroke="blue" fill="none"/>
<path stroke="red">
<animate attributeName="d" calcMode="linear" from="M 25 175 L 25 25" to="M 25 25 L 175 25" begin="0s" dur="6s" repeatCount="indefinite"/>
</path>
<path stroke="red">
<animate attributeName="d" calcMode="linear" from="M 25 25 L 175 25" to="M 175 25 L 175 175" begin="0s" dur="6s" repeatCount="indefinite"/>
</path>
<circle fill="red" r="5">
<animateMotion path="M 25 175 Q 25 25 175 25" begin="0s" dur="6s" calcMode="linear" repeatCount="indefinite"/>
</circle>
<circle fill="red" r="5">
<animateMotion path="M 25 25 Q 175 25 175 175" begin="0s" dur="6s" calcMode="linear" repeatCount="indefinite"/>
</circle>
<circle fill="red" r="5">
<animateMotion path="M 25 175 C 25 25 175 25 175 175" begin="0s" dur="6s" calcMode="linear" repeatCount="indefinite"/>
</circle>
</svg>
from svg要素の基本的な使い方まとめ
MITライセンス、著作権はdefghi1977に帰属
同じような事をどこかに書いた気がしたが見つからなかったtakker.icon
#2022-01-13 12:13:16
#2021-06-13 20:11:33
#2020-12-03 03:06:50
#2020-10-23